package battlebarnapplet.util;

/**
 *
 * @author johncreecy
 */
import java.net.*;
import java.util.*;
import java.io.*;
import java.nio.*;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class Util {

	public void getInterfaces() {
		try {
			Enumeration e = NetworkInterface.getNetworkInterfaces();

			while (e.hasMoreElements()) {
				NetworkInterface ni = (NetworkInterface) e.nextElement();
				// System.out.println("Net interface: " + ni.getName());

				Enumeration e2 = ni.getInetAddresses();

				while (e2.hasMoreElements()) {
					InetAddress ip = (InetAddress) e2.nextElement();
					// System.out.println("IP address: " + ip.toString());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getPrimaryInterface() {
		try {
			java.net.InetAddress i = java.net.InetAddress.getLocalHost();
			return (i.getHostAddress());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public static void playClip(URL soundFile) throws IOException, UnsupportedAudioFileException, LineUnavailableException {
		AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundFile);
		try {
			Clip clip = AudioSystem.getClip();
			clip.open(audioInputStream);
			try {
				clip.start();
				clip.drain();
			} finally {
				clip.close();
			}
		} finally {
			audioInputStream.close();
		}
	}

}
